#!/bin/bash

#COMANDOS
CMD_CAT=$(type -p cat)
CMD_GREP=$(type -p grep)
CMD_AWK=$(type -p awk)
CMD_WGET=$(type -p wget)
CMD_MV=$(type -p mv)
CMD_MYSQL=$(type -p mysql)
CMD_TAIL=$(type -p tail)
CMD_HEAD=$(type -p head)
CMD_DU=$(type -p du)

#DIRETORIO DE LOGS
DIR_ARCHIVE="/export/vmlogs_impar /export/vmlogs_par"
DIR_ARCHIVE_LOGS="/export/logs/vmlogs_archive"
DIR_EQUIPES="AA AC EC SB EMAIL"

#DATA DO LOG
DATALOG=$(/bin/date -d '0 day ago' +%Y-%m-%d)

#ARQUIVOS DE CONF
APP_HOSTS_I=/export/scripts/vmlogs_importer_i.conf
APP_HOSTS_P=/export/scripts/vmlogs_importer_p.conf

#DADOS PARA CONECTAR NO BANCO
APP_DB_USER=root
APP_DB_PASS=""
APP_DB_HOST=localhost
APP_DB_BASE=vmlogs


VerificaMaquina() {
	ID_SERVIDOR=$(${CMD_MYSQL} -u ${APP_DB_USER} -h ${APP_DB_HOST} -D ${APP_DB_BASE} -N -e "SELECT id FROM servidores WHERE nome_host='$1' AND equipe = '$2' AND caminho = '$3'")
	if [ "${ID_SERVIDOR}" = "" ]; then
		${CMD_MYSQL} -u ${APP_DB_USER} -h ${APP_DB_HOST} -D ${APP_DB_BASE} -e "INSERT INTO servidores (nome_host, equipe, caminho, data_insercao) VALUES ('$1','$2','$3','${DATALOG}')"
	fi
}

		


rodape() {
	echo ""
	echo "--"
	echo ""
}

# ENTRA NOS VMDIR
for DIRS in ${DIR_ARCHIVE}; do
	for EQDIR in $DIR_EQUIPES; do
		for SERVER in $(ls -1 ${DIRS}/${EQDIR}/${MAQUINAS}); do
			VerificaMaquina $SERVER $EQDIR $DIRS
			ID_SERVIDOR=$(${CMD_MYSQL} -u ${APP_DB_USER} -h ${APP_DB_HOST} -D ${APP_DB_BASE} -N -e "SELECT id FROM servidores WHERE nome_host='$SERVER' AND equipe = '$EQDIR' AND caminho = '$DIRS'")
			cd ${DIRS}/${EQDIR}/${SERVER}
			${CMD_DU} -sb logs/ logs_gz/ > ${DIR_ARCHIVE_LOGS}/${SERVER}_${DATALOG}.log
			if [ $? != 0 ]; then
				echo "servidor nao possui o diretorio logs ou logz_gz"
			fi
			LOGSREPORT=$(${CMD_CAT} ${DIR_ARCHIVE_LOGS}/${SERVER}_${DATALOG}.log |${CMD_GREP} "logs/" |${CMD_AWK} '{print $1}')
			LOGS_GZREPORT=$(${CMD_CAT} ${DIR_ARCHIVE_LOGS}/${SERVER}_${DATALOG}.log |${CMD_GREP} "logs_gz/" |${CMD_AWK} '{print $1}')
			
			CONSULTALOGS=$(${CMD_MYSQL} -u ${APP_DB_USER} -h ${APP_DB_HOST} -D ${APP_DB_BASE} -e "SELECT * FROM servidores_logs WHERE nome_host = '${ID_SERVIDOR}' AND particao = '0' AND data_insercao = '${DATALOG}'" |${CMD_TAIL} -n1)
			
			if [ "${CONSULTALOGS}" = "" ]; then
				${CMD_MYSQL} -u ${APP_DB_USER} -h ${APP_DB_HOST} -D ${APP_DB_BASE} -e "INSERT INTO servidores_logs (nome_host, particao, tamanho, data_insercao) VALUES ('${ID_SERVIDOR}','0','${LOGSREPORT}','${DATALOG}')"
			else
				echo "${SERVER}[logs]: Os dados do dia `echo ${DATALOG} | ${CMD_AWK} -F "-" '{print $3"/"$2"/"$1}'` ja estao no banco"
			fi
			
			CONSULTALOGSGZ=$(${CMD_MYSQL} -u ${APP_DB_USER} -h ${APP_DB_HOST} -D ${APP_DB_BASE} -e "SELECT * FROM servidores_logs WHERE nome_host = '${ID_SERVIDOR}' AND particao = '1' AND data_insercao = '${DATALOG}'" |${CMD_TAIL} -n1)

			if [ "${CONSULTALOGSGZ}" = "" ]; then
				${CMD_MYSQL} -u ${APP_DB_USER} -h ${APP_DB_HOST} -D ${APP_DB_BASE} -e "INSERT INTO servidores_logs (nome_host, particao, tamanho, data_insercao) VALUES ('${ID_SERVIDOR}','1','${LOGS_GZREPORT}','${DATALOG}')"
			else
				echo "${SERVER}[logs_gz]: Os dados do dia `echo ${DATALOG} | ${CMD_AWK} -F "-" '{print $3"/"$2"/"$1}'` ja estao no banco"
			fi
			
			rodape
		done
	done
done

